<?php

namespace app\api\logic;

use app\common\exception\BusinessException;
use think\Db;
use think\facade\Db as FacadeDb;

class Exam
{
    /**
     * 查询列表
     * @param array $param
     * @return array
     * @throws
     */
    public function getList($param = []): array
    {
        // 查询条件
        $where = [['status', '=', 1]];

        // 数据模型
        $examModel = new \app\common\model\Exam();

        // 查询字段
        $field = 'id,count,name';

        // 排序方式
        $order = ['id' => 'asc', 'sort' => 'asc'];

        // 查询数据
        $list = $examModel->field($field)->where($where)->order($order)->select();

        // 返回
        return $list ? $list->toArray() : [];
    }
    
    /**
     * 查询考试类型的专业和课程
     * @param array $param 参数，包含user_id等
     * @return array
     * @throws
     */
    public function getTypeMajor($param = []): array
    {
        // 提取参数
        $userId = $param['userId'];
        $examId = $param['exam_id'];

        // 验证考试ID是否存在
        $examModel = new \app\common\model\Exam();
        $exam = $examModel->where('id', $examId)->where('status', 1)->find();
        if (!$exam) {
            return [];
        }
        
        // 使用模型关联查询，一次性获取所有数据
        $professionModel = new \app\common\model\Profession();
        $professions = $professionModel->alias('p')
            ->leftJoin ('profession_course pc', 'p.id = pc.profession_id')
            ->leftJoin ('course c', 'pc.course_id = c.id')
            ->leftJoin ('collect_course cc', 'c.id = cc.course_id AND cc.user_id = ' . (int)$userId . ' AND cc.status = 1')
            ->where('p.exam_id', $examId)
            ->where('p.status', 1)
            ->where('c.status', 1)
            ->field('p.id as profession_id, p.name as profession_name, p.code as profession_code, 
                   c.id as course_id, c.name as course_name, c.code as course_code')
            ->order('p.id asc,c.id asc')
            ->select();
            
        // 打印SQL
        //var_dump(FacadeDb::getLastSql());
        //die;

        $result = [];
        if ($professions) {
            $professionList = [];
            $courseMap = [];
            
            // 整理数据，按专业分组
            foreach ($professions->toArray() as $item) {
                $professionId = $item['profession_id'];
                
                // 如果专业还没有添加到列表中，则添加
                if (!isset($professionList[$professionId])) {
                    $professionList[$professionId] = [
                        'id' => $professionId,
                        'name' => $item['profession_name'],
                        'code' => $item['profession_code'],
                        'courses' => []
                    ];
                }
                
                // 添加课程到专业的课程列表中
                $professionList[$professionId]['courses'][] = [
                    'id' => $item['course_id'],
                    'name' => $item['course_name'],
                    'code' => $item['course_code']
                ];
            }
            
            // 将关联数组转换为索引数组
            $result = [
                'exam_id' => $examId,
                'exam_name' => $exam['name'],
                'professions' => array_values($professionList)
            ];
        }
        
        return $result;
    }
}